<?php

namespace app\admin\utils\controller;

use app\admin\app\model\SystemFile;
use app\admin\etf\model\EtfGroupCode;
use app\common\controller\BaseController;
use plugin\admin\app\common\Tree;
use plugin\admin\app\model\Admin;
use plugin\user\app\model\User;
use support\Db;
use support\facade\Logger;
use support\Request;

class FormatController extends BaseController
{
    /**
     * 不需要鉴权的方法
     * @var string[]
     */
    protected $noNeedAuth = ['index'];

    public $formatData = [
        // 直接查询数据库的配置方式
        'admins'=>['name'=>'管理员名称','table'=>'admins','prefix'=>'wa_','nameField'=>'username','idField'=>'id','type'=>'select','where'=>''],
        'users_nickname'=>['name'=>'用户昵称','table'=>'users','prefix'=>'wa_','nameField'=>'nickname','idField'=>'id','type'=>'select','where'=>''],
        'users_username'=>['name'=>'用户名称','table'=>'users','prefix'=>'wa_','nameField'=>'username','idField'=>'id','type'=>'select','where'=>''],




//        'etf_group'=>['name'=>'ETF组合','table'=>'group_list','prefix'=>'etf_','nameField'=>'group_name','idField'=>'id','type'=>'select','where'=>''],
//        'etf_code'=>['name'=>'ETF代码','table'=>'lists','prefix'=>'etf_','nameField'=>'name','idField'=>'code','type'=>'select','where'=>''],

        // 如果是type = tree 的话， 则还需要指定 pid 字段名
//        'users_username'=>['name'=>'用户名称','table'=>'users','prefix'=>'wa_','nameField'=>'username','idField'=>'id','pidField'=>'pid','type'=>'tree','where'=>''],


        // 直接调用本类方法的配置   action 是方法名  type 是最终渲染的类型方式  where 是额外参数
        'let_demo'=>['name'=>'类admins方法调用','action'=>'admins','type'=>'select','where'=>'','table'=>'类方法'],
        'let_fileExt'=>['name'=>'文件后缀列表','action'=>'fileExt','type'=>'select','where'=>'','table'=>'类方法'],
        'etf_group_code'=>['name'=>'ETF组合代码','action'=>'groupCode','type'=>'select','where'=>'','table'=>'类方法'],


        // 其他基础控件 的使用
        'form-input1'=>['name'=>'输入框-必填','text'=>'lay-verify:required;placeholder:请输入','type'=>'form','table'=>'Form静态表单'],
        'form-input2'=>['name'=>'输入框-选填','text'=>'placeholder:请输入','type'=>'form','table'=>'Form静态表单'],
        'form-input3'=>['name'=>'输入框-手机','text'=>'placeholder:请输入','type'=>'form','table'=>'Form静态表单'],
        'form-input4'=>['name'=>'输入框-邮箱','text'=>'placeholder:请输入','type'=>'form','table'=>'Form静态表单'],
        'form-textArea'=>['name'=>'多行文本','text'=>'lay-verify:required;placeholder:请输入','type'=>'form','table'=>'Form静态表单'],
//        'form-select'=>['name'=>'下拉选择框','text'=>'data:0:正常,1:禁用,2:审核;value:0','type'=>'form','table'=>'Form静态表单'],// 这个基本用字典或者 动态参数较多

        'form-switch'=>['name'=>'开关选项','text'=>'lay-text:开启|关闭;value:1','type'=>'form','table'=>'Form静态表单'],
        'form-upload'=>['name'=>'文件上传','text'=>'url:/app/admin/upload/file;exts:zip','type'=>'form','table'=>'Form静态表单'],
        'form-uploadImg'=>['name'=>'图片上传','text'=>'url:/app/admin/upload/avatar;size:2000','type'=>'form','table'=>'Form静态表单'],
        'form-icon'=>['name'=>'图标选择','text'=>'value:layui-icon-set','type'=>'form','table'=>'Form静态表单'],

        'form-datetime'=>['name'=>'日期时间选择','text'=>'format:datetime;type:datetime;','type'=>'form','table'=>'Form时间日期组件'],
        'form-date'=>['name'=>'日期选择','text'=>'format:date;type:date','type'=>'form','table'=>'Form时间日期组件'],
        'form-time'=>['name'=>'时间选择','text'=>'format:datetime;type:time;','type'=>'form','table'=>'Form时间日期组件'],

    ];
    // 动态参数配置  key 参数

    function index(Request $request){
        $pathUrl = 'url:/admin/utils/format/index?format=';
        $data = $this->formatData;
        if($format = $request->get('format')){
            foreach ($data as $key => $item){
                if($key == $format){
                    if($item['prefix']){
                        // 直接查询库
                        $query = Db::table($item['prefix'].$item['table'])->select($item['nameField'],$item['idField']);
                        if($item['where']){
                            $query->where($item['where']);
                        }
                        $data = $query->get()
                            ->map(function ($item){
                                return (array)$item;
                            })->all();
                    }else{
                        // 其他的实现方式
//                        $data = $this->$item['action']($item['where']);
                        return call_user_func([$this, $item['action']],[$item['where']]);
                    }

                    // 有数据
                    if($data){
                        if($item['type']=='select'){
                            // 构造为下拉列表框的数据结构
                            return $this->setSelect($data,$item['nameField'],$item['idField']);
                        }else{
                            // 构造为树状图的结构
                            return $this->setTree($data,$item['nameField'],$item['idField'],$item['pidField']);
                        }
                    }

                }
            }
            return  $this->success( 'ok');
        }

        // 对数据进行分组
        $newData = [];
        foreach ($data as $key => $item){
            if(!empty($item['prefix'])){
                $newData['表名：'.$item['table']][$key] = $item;
            }else{
                $newData[$item['table']][$key] = $item;
            }
        }
        \support\View::assign([
            'list'=>$newData,
            'path'=>$pathUrl,
        ]);
        return view('utils/format/index');
    }

    /**
     * 下拉选择框 动态参数示例
     * @return \support\Response
     */
    protected function admins(){
        $data = Admin::select('id,username');
        return $this->setSelect($data,'username','id');
    }



    /**
     * 文件上传后缀名
     * @return \support\Response
     */
    function fileExt(){
        $data = SystemFile::groupBy('xext')->select('xext')->get();
        Logger::debug($data);
        return $this->setSelect($data,'xext','xext');
    }



    /**
     * 下拉选择框 的实现
     * @param $items 数据
     * @param $nameField 显示的名称
     * @param $valueField 对应的值
     * @return \support\Response
     */
    protected function setSelect($items,$nameField,$valueField='id'){
        $formatted_items = [];
        foreach ($items as $item) {
            $formatted_items[] = [
                'name' => $item[$nameField],
                'value' => $item[$valueField]
            ];
        }
        return  $this->json(0, 'ok', $formatted_items);
    }

    /**
     * 下拉树状图的实现
     * @param $items 数据列表
     * @param $nameField 显示的名称
     * @param $valueField 对应的值
     * @param $pidField 父级ID
     * @return \support\Response
     */
    protected function setTree($items,$nameField,$valueField='id',$pidField='pid'){
        $format_items = [];
        foreach ($items as $item) {
            $format_items[] = [
                'name' => $item[$nameField] ?? $item[$valueField],
                'value' => (string)$item[$valueField],
                'id' => $item[$valueField],
                'pid' => $item[$pidField],
            ];
        }
        $tree = new Tree($format_items);
        return $this->json(0, 'ok', $tree->getTree());
    }

}